package com.tansun.easycare.rule.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

import javax.sql.rowset.serial.SerialClob;

import com.tansun.easycare.rule.exception.BizException;

public class DBUtils {
	
	public static String ClobToString(Object clob) {
		String ret = "";
	    ret = (String)clob;
		return ret;
	}
	
	public static String ClobToString(Clob clob) {
		String ret = "";
		try {
			Reader read = clob.getCharacterStream();
			BufferedReader br = new BufferedReader(read);
			String s = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (s != null) {
				sb.append(s+"\n");
				s = br.readLine();
			}
			ret = sb.toString();
			if (br != null) {
				br.close();
			}
			if (read != null) {
				read.close();
			}
		} catch (SQLException | IOException e) {
			throw new BizException(e,"Clob ==> String 类型转换异常");
		}
		return ret;
	}

	public static Clob StringToClob(String content) {
		Clob clob = null;
		try {
			clob = new SerialClob(content.toCharArray());
		} catch (Exception e) {
			throw new BizException(e,"String ==> Clob类型转换异常");
		}
		return clob;
	}

}
